package com.slkj.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.slkj.dto.CauseTaskDTO;
import com.slkj.dto.Result;
import com.slkj.dto.StatusCode;
import com.slkj.entity.CauseTask;
import com.slkj.entity.City;
import com.slkj.entity.CityAudit;
import com.slkj.entity.File;
import com.slkj.service.CauseService;
import com.slkj.service.CityService;
import com.slkj.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Transactional
@RestController
@RequestMapping("${address.prefix}/cause")
public class CauseController {
    @Autowired
    CauseService causeService;

    @Autowired
    UserService userService;

    private Logger log = LoggerFactory.getLogger(SpecialController.class);


    @PostMapping("/createTask")
    public Result createTask(@RequestBody CauseTask causeTask) {

        log.info("----------------------->>>createTask(@RequestBody SpecialTask specialTask) ");
        if (causeTask == null) {
            return new Result(false, StatusCode.ERROR, "参数为空");
        }

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");


        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }
        causeService.createTask(causeTask, username);
        return new Result(true, StatusCode.OK, "success");
    }

    @PostMapping("/getTaskFeedback")
    public Result showAll(@RequestBody String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        String title = jsonObject.getString("title");
        String light = jsonObject.getString("light");
        String userId = jsonObject.getString("userId");
        String status = jsonObject.getString("status");

        if (StringUtils.isNotBlank(status)) {
            status = "通过";
        }


        List<String> projectTime = new ArrayList<>();


        Date beginDate = null;
        Date endDate = null;

        if (StringUtils.isNotBlank(jsonObject.getString("projectTime"))) {
            projectTime = JSONObject.parseArray(jsonObject.getString("projectTime"), String.class);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            try {
                beginDate = simpleDateFormat.parse(projectTime.get(0));
                endDate = simpleDateFormat.parse(projectTime.get(1));
            } catch (ParseException e) {
                return new Result(false, StatusCode.ERROR, "日期格式转换异常");
            }
        } else if (StringUtils.isNotBlank(jsonObject.getString("month"))) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            try {
                beginDate = simpleDateFormat.parse(jsonObject.getString("month") + "/01");
                endDate = simpleDateFormat.parse(jsonObject.getString("month") + "/31");
            } catch (ParseException e) {
                return new Result(false, StatusCode.ERROR, "日期格式转换异常");
            }

        }
        String gotoPageString = jsonObject.getString("page");
        String pageSizeString = jsonObject.getString("pageSize");

        log.info("---------------------------------------------->>>>>>>>获取title=" + title);
        log.info("---------------------------------------------->>>>>>>>获取beginDate=" + beginDate + toString());
        log.info("---------------------------------------------->>>>>>>>获取light=" + light);
        log.info("---------------------------------------------->>>>>>>>获取endDate=" + endDate + toString());
        log.info("---------------------------------------------->>>>>>>>获取gotoPageString=" + gotoPageString);
        log.info("---------------------------------------------->>>>>>>>获取pageSizeString=" + pageSizeString);


        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");


        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }
        int gotoPage = 1;

        if (StringUtils.isNotBlank(gotoPageString) && StringUtils.isNumeric(gotoPageString)) {
            gotoPage = Integer.parseInt(gotoPageString);
        }

        int pageSize = 10;

        if (StringUtils.isNotBlank(pageSizeString) && StringUtils.isNumeric(pageSizeString)) {
            pageSize = Integer.parseInt(pageSizeString);
        }
        PageInfo<CauseTaskDTO> causeTaskDTOPageInfo = causeService.getTaskFeedback(title, userId, light, beginDate, endDate, gotoPage, pageSize, username, status);

        if (causeTaskDTOPageInfo == null) {
            log.error("查询绩效事业单位数据,无数据");
            return new Result(true, StatusCode.NULL, "无数据");
        }
        if (causeTaskDTOPageInfo.getSize() == 0) {
            log.error("查询绩效事业单位数据,无数据");
            return new Result(true, StatusCode.NULL, "无数据");
        }


        log.info("查询绩效事业单位数据,返回View");
        return new Result(true, StatusCode.OK, "查询绩效事业单位数据", causeTaskDTOPageInfo);


    }

    //打分
    @PostMapping("/point")
    public Result point(@RequestBody String data) {
        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }
        JSONObject jsonObject = JSON.parseObject(data);
        String point = jsonObject.getString("point");
        List<String> dutyId = JSONObject.parseArray(jsonObject.getString("dutyId"), String.class);
        if (StringUtils.isBlank(point) || !StringUtils.isNumeric(point)) {
            return new Result(false, StatusCode.ERROR, "参数异常");
        }
        causeService.point(point, dutyId);
        return new Result(true, StatusCode.OK, "success");
    }


    //通过
    @PostMapping("/passTask")
    public Result passTask(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        List<String> dutyIdList = JSONObject.parseArray(jsonObject.getString("dutyId"), String.class);
        String count = jsonObject.getString("count");
        List<Map> variables = JSONObject.parseArray(jsonObject.getString("variables"), Map.class);
        if (StringUtils.isBlank(count) || variables == null) {
            return new Result(false, StatusCode.ERROR, "参数为空");
        }
        if (!StringUtils.isNumeric(count)) {
            return new Result(false, StatusCode.ERROR, "参数异常");
        }

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");

        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }
        causeService.audit(dutyIdList, variables, count);
        return new Result(true, StatusCode.OK, "success");
    }


    //驳回
    @RequestMapping("/noPassTask")
    public Result worry(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        List<String> dutyIdList = JSONObject.parseArray(jsonObject.getString("dutyId"), String.class);
        String count = jsonObject.getString("count");
        List<Map> variables = JSONObject.parseArray(jsonObject.getString("variables"), Map.class);
        if (StringUtils.isBlank(count) || variables == null) {
            return new Result(false, StatusCode.ERROR, "参数为空");
        }
        if (!StringUtils.isNumeric(count)) {
            return new Result(false, StatusCode.ERROR, "参数异常");
        }

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");

        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }
        causeService.worry(dutyIdList, variables, count);
        return new Result(true, StatusCode.OK, "success");
    }

    //亮灯调整
    @RequestMapping("/lightAdjust")
    public Result whyAll(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        List<String> dutyIdList = JSONObject.parseArray(jsonObject.getString("dutyId"), String.class);
        String light = jsonObject.getString("light");
        String why = jsonObject.getString("why");
        List<Map> variables = JSONObject.parseArray(jsonObject.getString("variables"), Map.class);
        if (variables == null) {
            return new Result(false, StatusCode.ERROR, "参数为空");
        }


        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");

        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }
        causeService.why(light, why, dutyIdList, variables);
        return new Result(true, StatusCode.OK, "success");
    }

    @PostMapping("/taskUpload")
    public Result taskUpload(@RequestBody String data) throws SQLException {
        JSONObject jsonObject = JSONObject.parseObject(data);
        String count = jsonObject.getString("count");
        List<Map> variables = JSONObject.parseArray(jsonObject.getString("variables"), Map.class);
        List<File> fileList = JSONObject.parseArray(jsonObject.getString("file"), File.class);
        if (StringUtils.isBlank(count)) {
            return new Result(false, StatusCode.ERROR, "参数为空");
        }
        if (!StringUtils.isNumeric(count)) {
            return new Result(false, StatusCode.ERROR, "参数异常");
        }
        if (fileList == null) {
            return new Result(false, StatusCode.ERROR, "参数为空");
        }

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
        String dutyId = jsonObject.getString("dutyId");

        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }
        causeService.taskUpload(fileList, dutyId, variables, count);
        return new Result(true, StatusCode.OK, "success");
    }


}
